﻿using Report_Dal.Web;
using Report_IDal.Web;
using Report_IService.Web;
using Report_Model.Db;
using Report_Model.Web;
using Report_Service.Basic;
using Report_Unity.Basic;
using Report_Unity.Unitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Report_Service.Web
{
    public class T_HospitalService : BaseService<T_Hospital>, IT_HospitalService
    {
        private IT_HospitalDal T_HospitalDal = new T_HospitalDal(ConfigSource.tab_web);
        private IT_Ticket_RecordDal T_Ticket_RecordDal = new T_Ticket_RecordDal(ConfigSource.tab_web);

        public override void SetCurrentDal()
        {
            base.BaseDal = T_HospitalDal;
        }

        public ResultInfo AddHospital(string name, string desc, string userid)
        {
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(name)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(desc)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            T_Hospital hos = GetT_Hospital(name, desc, userid);

            if (!Add(hos))
                return CodeStatus.error_301.GetStatus("添加失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo EditHospital(string hid, string name, string desc, string userid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(name)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(desc)}必传");
            if (string.IsNullOrEmpty(name))
                return CodeStatus.error_201.GetStatus($"{nameof(userid)}必传");

            T_Hospital hos = Get(a => a.hospital_id == hid).FirstOrDefault();
            if (hos == null)
                return CodeStatus.error_202.GetStatus("无医院");

            hos.hospital_name = name;
            hos.hospital_desc = desc;
            hos.up_userid = userid;
            hos.up_time = DateTime.Now;

            if (!Up(hos))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo DelHospital(string hid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");

            T_Hospital hos = Get(a => a.hospital_id == hid).FirstOrDefault();
            if (hos == null)
                return CodeStatus.error_202.GetStatus("无医院");

            if (!Del(hos))
                return CodeStatus.error_301.GetStatus("删除失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo GetHospital(string hid)
        {
            var hos = GetAsNoTracking(a => a.hospital_name == hid).Select(a => new
            {
                hid = a.hospital_id,
                name = a.hospital_name,
                desc = a.hospital_desc,
                login_name = a.login_name
            }).FirstOrDefault();

            if (hos == null)
                return CodeStatus.error_202.GetStatus("无医院信息");

            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = hos;

            return res;
        }

        public ResultInfo GetListHospital()
        {
            var list = GetAsNoTracking(a => true).Select(a => new
            {
                hid = a.hospital_id,
                name = a.hospital_name,
                desc = a.hospital_desc,
            }).ToList();
            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = new { list = list };
            return res;
        }

        public ResultInfo EditLoginInfo(string hid,string username,string pwd,string repeat_pwd)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");

            if (string.IsNullOrEmpty(username))
                return CodeStatus.error_201.GetStatus($"{nameof(username)}必传");

            if (string.IsNullOrEmpty(pwd))
                return CodeStatus.error_201.GetStatus($"{nameof(pwd)}必传");

            if (string.IsNullOrEmpty(repeat_pwd))
                return CodeStatus.error_201.GetStatus($"{nameof(repeat_pwd)}必传");

            if (pwd != repeat_pwd)
                return CodeStatus.error_205.GetStatus();

            T_Hospital hos = T_HospitalDal.Get(a => a.hospital_id == hid).FirstOrDefault();
            if (hos == null)
                return CodeStatus.error_202.GetStatus("无医院信息");

            hos.login_name = username;
            hos.login_password = DesUtils.Encrypt(pwd);

            if (!Up(hos))
                return CodeStatus.error_301.GetStatus("修改失败");

            return CodeStatus.success.GetStatus();
        }

        public ResultInfo Login(string hid, string username, string pwd, string ticket, string code)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");

            if (string.IsNullOrEmpty(username))
                return CodeStatus.error_201.GetStatus($"{nameof(username)}必传");

            if (string.IsNullOrEmpty(pwd))
                return CodeStatus.error_201.GetStatus($"{nameof(pwd)}必传");

            if (string.IsNullOrEmpty(ticket))
                return CodeStatus.error_201.GetStatus($"{nameof(ticket)}必传");
            Guid gid;
            if (!Guid.TryParse(ticket, out gid))
                return CodeStatus.error_201.GetStatus($"{nameof(ticket)}格式不正确");

            if (string.IsNullOrEmpty(code))
                return CodeStatus.error_201.GetStatus($"{nameof(code)}必传");

            T_Hospital hos = T_HospitalDal.GetAsNoTracking(a => a.hospital_id == hid).FirstOrDefault();
            if (hos == null)
                return CodeStatus.error_202.GetStatus("无医院信息");

            T_Ticket_Record ticketRecord = T_Ticket_RecordDal.GetAsNoTracking(a => a.check_ticket == gid).FirstOrDefault();
            if (ticketRecord == null)
                return CodeStatus.error_203.GetStatus("失效");
            if (ticketRecord.check_code != code)
                return CodeStatus.error_203.GetStatus("不正确");

            if (hos.login_name != username || hos.login_password != DesUtils.Encrypt(pwd))
                return CodeStatus.error_204.GetStatus();

            object objResult = new
            {
                hid = hid,
                name = hos.hospital_name,
            };

            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = objResult;
            return res;
        }

        private T_Hospital GetT_Hospital(string name, string desc, string userid)
        {
            T_Hospital hos = new T_Hospital();
            hos.hospital_id = GetNextId(a => a.hospital_id);
            hos.hospital_name = name;
            hos.hospital_desc = desc;
            hos.create_userid = userid;
            hos.create_time = DateTime.Now;
            SuperUser superUser = new SuperUser();
            hos.login_name = superUser.username;
            hos.login_password = DesUtils.Encrypt(superUser.pwd);
            return hos;
        }
    }
}
